﻿using Marmat.Forms.Local;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Forms
{
    public partial class RelationSettingForm : Form
    {

        private Marmat.Forms.Local.DataGrid _bDataGrid;

        public RelationSettingForm(Marmat.Forms.Local.DataGrid bDataGrid)
        {
            InitializeComponent();
            this._bDataGrid = bDataGrid;

            this.bComboBox2.Items.Add(SyncLoadModeEnum.None);
            this.bComboBox2.Items.Add(SyncLoadModeEnum.Filter);
            this.bComboBox2.Items.Add(SyncLoadModeEnum.Load);

            DataGrid[] dataGrids = getBDataGrid(bDataGrid.FindForm().Controls);
            foreach (var dataGrid in dataGrids)
            {
                if (bDataGrid != dataGrid)
                {
                    this.bComboBox1.Items.Add(dataGrid);
                }
            }

            this.bComboBox1.Value = bDataGrid.Relation.Table;
            this.bComboBox2.Value = bDataGrid.Relation.SyncLoadMode;
            foreach (var relation in bDataGrid.Relation.Items)
            {
                Marmat.Forms.Views.DataGrid.Row row = this.BDataGrid1.AddNew();
                row["父表关联列"] = relation.ParentName;
                row["子表关联列"] = relation.ChildName;
            }
            this.BDataGrid1.AcceptChanges();

            DataGrid[] getBDataGrid(System.Windows.Forms.Control.ControlCollection collection)
            {
                List<DataGrid> items = new List<DataGrid>();
                foreach (System.Windows.Forms.Control control in collection)
                {
                    if (control is DataGrid dataGrid)
                        items.Add(dataGrid);
                    else if (control.HasChildren)
                    {
                        items.AddRange(getBDataGrid(control.Controls));
                    }
                }
                return items.ToArray();
            }
        }

        private void BDataGrid1_CellNewRowLink(object sender, EventArgs e)
        {
            this.BDataGrid1.AddNew();
        }

        private void BDataGrid1_CellLinkClick(object sender, Views.DataGrid.RowColEventArgs e)
        {
            if (System.Windows.Forms.MessageBox.Show("确定要删除该条记录吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                e.Row.Delete();
            }
        }

        private void BButton1_Click(object sender, EventArgs e)
        {
            this.bComboBox1.Value = null;
            this.bComboBox2.Value = SyncLoadModeEnum.None;
            this.BDataGrid1.Rows.Clear();
        }

        private void BComboBox1_CustomDisplayText(object sender, CustomDisplayTextEventArgs e)
        {
            if (e.Value != null)
                e.DisplayText = (e.Value as System.ComponentModel.Component).Site.Name;
        }
    }
}
